package com.changgou.service.goods.controller;


import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.service.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/brand")
@RestController
public class BrandController {

    @Autowired
    private BrandService brandService;

    //查询所有品牌信息
    @GetMapping("/list")
    public Result<List<Brand>> findAll(){
        List<Brand> list = brandService.findAll();
        return new Result<>(true, StatusCode.OK,"操作成功",list);
    }

    //根据id查询品牌
    @GetMapping("/findById/{id}")
    public Result findById(@PathVariable int id){
        Brand brand = brandService.findById(id);
        return new Result(true,StatusCode.OK,"操作成功",brand);
    }

    //添加品牌数据
    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    //根据id删除品牌
    @DeleteMapping("/{id}")
    public Result delById(@PathVariable int id){
        brandService.delById(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    //根据id修改品牌
    @PutMapping("/{id}")
    public Result updateById(@RequestBody Brand brand, @PathVariable("id") int id){
        brand.setId(id);
        brandService.updateById(brand);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    //分页展示
    @GetMapping("/page/{pageNumber}/{pageSize}")
    public Result page(@PathVariable("pageNumber") int pageNumber,@PathVariable("pageSize") int pageSize){
        Page<Brand> page = brandService.page(pageNumber, pageSize);
        List<Brand> result = page.getResult();
        long total = page.getTotal();
        PageResult pageResult = new PageResult(total,result);
        return new Result(true,StatusCode.OK,"分页成功",pageResult);
    }

    //根据用户输入的品牌进行查询
    @GetMapping("/search/{str}")
    public Result selectByCondition(@PathVariable("str") String str){
        Page<Brand> brands = brandService.selectByCondition(str);
        return new Result(true,StatusCode.OK,"查询成功",brands);
    }
}
